package com.z.admin.controller;


import com.z.common.base.PageTableRequest;
import com.z.common.base.ResponseCode;
import com.z.common.base.Results;
import com.z.admin.entity.Role;
import com.z.admin.service.IRoleService;
import com.z.admin.vo.RoleDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author z
 * @since 2022-02-13
 */
@Controller
@RequestMapping("/admin/role")
@Api(tags = "角色相关")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @GetMapping("all")
    @ResponseBody
    public Results all(PageTableRequest page) {
        List<Role> results = roleService.findAll();
        return Results.success(results.size(), results);
    }
    @GetMapping("list")
    @ApiOperation("角色列表分页查询")
    @ResponseBody
    public Results list(PageTableRequest page) {
        page.countOffset();
        Results<Role> results = roleService.findByPage(page.getOffset(), page.getLimit());
        return results;
    }
    @GetMapping("listByMap")
    @ApiOperation("角色列表条件分页查询")
    @ResponseBody
    public Results listByMap(PageTableRequest page, HttpServletRequest request) {
        page.countOffset();

        Map<String, Object> map = new HashMap<>();
        String rolename = request.getParameter("rolename");
        if(!StringUtils.isEmpty(rolename))
            map.put("name_like_s", rolename);
        String id = request.getParameter("id");
        if(!StringUtils.isEmpty(id))
            map.put("id_eq_s", id);
        String phone = request.getParameter("phone");
        if(!StringUtils.isEmpty(phone))
            map.put("phone_eq_s", phone);
        Results<Role> results = roleService.findPageByMap(map, page.getOffset(), page.getLimit());
        return results;
    }

    @PreAuthorize("hasAuthority('sys:role')")
    @GetMapping("toList")
    @ApiOperation("角色列表页面")
    public String toList() {
        return "admin/role/roleList";
    }

    @ApiOperation("角色添加页面")
    @GetMapping("toAdd")
    public String toAdd(Model model) {
        model.addAttribute("sysRole",new Role());
        return "admin/role/roleAdd";
    }
    @GetMapping("toEdit")
    @ApiOperation("角色编辑页面")
    public String toEdit(Model model, String id) {
        model.addAttribute("sysRole", roleService.findById(id));
        return "admin/role/roleEdit";
    }
    @PostMapping("add")
    @ApiOperation("角色添加接口")
    @ResponseBody
    public Results add(RoleDto role, Integer roleId){
        try{
            Role roleDB = roleService.findRoleByName(role.getName());
            if(roleDB != null ){
                throw new Exception("角色已存在");
            }
            role.setCreateTime(new Date());
            roleService.save(role);
        }catch (Exception e){
            e.printStackTrace();
            return Results.failure(ResponseCode.error.getCode(), e.getMessage());
        }
        return Results.success();
    }
    @PostMapping("update")
    @ApiOperation("角色更新接口")
    @ResponseBody
    public Results update(RoleDto role, Integer roleId){
        try{
            Role roleDB = roleService.findRoleByName(role.getName());
            if(roleDB != null && !(role.getId().equals(roleDB.getId()))){
                throw new Exception("角色已被占用！");
            }
            role.setUpdateTime(new Date());
            roleService.update(role, roleId);
        }catch (Exception e){
            e.printStackTrace();
            return Results.failure(ResponseCode.error.getCode(), e.getMessage());
        }
        return Results.success();
    }
    @PostMapping("del")
    @ApiOperation("角色删除-单个")
    @ResponseBody
    public Results del(String id){
        try{
            roleService.deleteById(id);
        }catch (Exception e){
            e.printStackTrace();
            return Results.failure(ResponseCode.error.getCode(), e.getMessage());
        }
        return Results.success();
    }
    @PostMapping("delAll")
    @ApiOperation("角色删除接口-多个")
    @ResponseBody
    public Results delAll(String ids){
        try{
            for(String id : ids.split(","))
                roleService.deleteById(id);
        }catch (Exception e){
            e.printStackTrace();
            return Results.failure(ResponseCode.error.getCode(), e.getMessage());
        }
        return Results.success();
    }

}